package com.zch.book.service.impl;

import java.util.ArrayList;
import java.sql.Date;
import java.util.List;

import com.zch.book.dao.BookDao;
import com.zch.book.dao.impl.BookDaoImpl;
import com.zch.book.entity.Book;
import com.zch.book.service.BookService;
import com.zch.book.util.DateFormat;

public class BookServiceImpl implements BookService {
	BookDao bd = new BookDaoImpl();
	//私有化一个不能改变的常数（每页显示的数据条数）
	private final int PAGE_SIZE = 4;
	@Override
	public List<Book> getBookByPage(String bookName, String bookAuthor, Date startBookDate,Date endBookDate,int pageIndex) {
		//使用lisit集合来存放模糊查询的参数
		List<Object> param = new ArrayList<Object>();
		//使用字符缓冲流来拼接模糊查询的sql语句,根据条件查询的时候先固化rownum,然后再利用rownum的值进行分页
		StringBuffer sb = new StringBuffer("select * from (select bk.*,rownum rn from (select b.* from book b where 1=1 ");
		if(bookName!=null&&bookName.length()>0){
			sb.append("and upper(b.bookname) like ? ");
			param.add("%"+bookName.toUpperCase()+"%");
		}
		if(bookAuthor!=null&&bookAuthor.length()>0){
			sb.append("and upper(b.bookauthor) like ? ");
			param.add("%"+bookAuthor.toUpperCase()+"%");
		}
		if(startBookDate!=null){
			sb.append("and b.bookdate >= ? ");
			param.add(startBookDate);
		}
		if(endBookDate!=null){
			sb.append("and b.bookdate <= ? ");
			param.add(endBookDate);
		}
		//完整的sql语句
		sb.append("order by bookno)bk)where rn>=? and rn<=?");
		//将得到的字符缓冲流转换为字符串
		String sql = sb.toString();
		System.out.println(sql);
		//得到当前页第一条数据在查询的总数据中的位置
		int firstRow = (pageIndex - 1) * PAGE_SIZE + 1;
		//得到当前页最后一条数据在查询的总数据中的位置
		int lastRow = pageIndex * PAGE_SIZE;
		//将得到该页的起始位置和末尾位置存入参数集合中
		param.add(firstRow);
		param.add(lastRow);
		//根据参数的个数来创建一个Object数组来存放上面集合中的参数
		Object[] params = new Object[param.size()];
		for(int i = 0; i < params.length; i++){
			params[i] = param.get(i);
		}
		//调用dao层的模糊查询方法获得图书列表
		return bd.getBookByPage(sql, params);
	}

	@Override
	public int getTotalCount(String bookName, String bookAuthor,Date startBookDate,Date endBookDate) {
		// 使用lisit集合来存放模糊查询的参数
		List<Object> param = new ArrayList<Object>();
		// 使用字符缓冲流来拼接模糊查询的sql语句,根据条件查询的时候先固化rownum,然后再利用rownum的值进行分页
		StringBuffer sb = new StringBuffer(
				"select count(*) from (select bk.*,rownum rn from (select b.* from book b where 1=1 ");
		if (bookName != null && bookName.length() > 0) {
			sb.append("and b.bookname like ? ");
			param.add("%" + bookName + "%");
		}
		if (bookAuthor != null && bookAuthor.length() > 0) {
			sb.append("and b.bookauthor like ? ");
			param.add("%" + bookAuthor + "%");
		}
		if(startBookDate!=null){
			sb.append("and b.bookdate >= ? ");
			param.add(startBookDate);
		}
		if(endBookDate!=null){
			sb.append("and b.bookdate <= ? ");
			param.add(endBookDate);
		}
		// 完整的sql语句
		sb.append("order by bookno)bk)");
		// 将得到的字符缓冲流转换为字符串
		String sql = sb.toString();
		System.out.println(sql);
		// 根据参数的个数来创建一个Object数组来存放上面集合中的参数
		Object[] params = new Object[param.size()];
		for (int i = 0; i < params.length; i++) {
			params[i] = param.get(i);
		}
		//调用dao层方法获得查询的总数据条数
		int counts = bd.getTotalCount(sql, params);
		//获得总页数
		int pageCounts = counts%PAGE_SIZE==0?counts/PAGE_SIZE:counts/PAGE_SIZE+1;
		return pageCounts;
	}

	@Override
	public Book getBookByBookNo(int bookNo) {
		return bd.getBookByBookNo(bookNo);
	}

	@Override
	public boolean addBook(Book book) {
		return bd.addBook(book);
	}

	@Override
	public boolean deleteBook(int bookNo) {
		return bd.deleteBook(bookNo);
	}

	@Override
	public boolean updateBook(Book book) {
		return bd.updateBook(book);
	}

}
